package edu.ksu.cis.android.project32;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.widget.Toast;

public class AntiTheftService extends Service implements SensorEventListener {
	private final float MAX_CHANGE_X = 0.000000002f;
	private final float MAX_CHANGE_Y = 0.000000002f;
	private final float MAX_CHANGE_Z = 0.000000002f;

	private Notification notification;
	protected static boolean isRunning = false;

	private float startMovementTime;
	private float prevMovementTime;
	private float[] accelSensors = {0,0,0};

	private SensorManager sensorManager;
	private Sensor accelerometer;

	private float mx = 0f;
	private float my = 0f;
	private float mz = 0f;

	protected static boolean alarmSounding = false;
	private static Thread alarm;
	private MediaPlayer mp;
	private WakeLock mWakeLock;
	private PowerManager mPowerManager;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	public void onCreate()
	{
		// Get an instance of the PowerManager
		mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
		// Create a bright wake lock
		mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, getClass()
				.getName());
		
		notification = new Notification(R.drawable.ic_launcher,"AntiTheft Running",System.currentTimeMillis());
		notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_NO_CLEAR;
		notification.setLatestEventInfo(getApplicationContext(), "AntiTheft", "Service is Running",
				PendingIntent.getActivity(this, 0, new Intent(this, AntiTheftMain.class), 0));

		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	}

	public int onStartCommand(Intent intent, int flags, int startId)
	{
		Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
		startForeground(10, notification);
		startMovementTime = prevMovementTime = System.nanoTime();

		sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);

		return START_STICKY;
	}


	public void onDestroy()
	{
		Toast.makeText(this, "service stopping", Toast.LENGTH_SHORT).show();
		isRunning = false;
		alarmSounding = false;
		sensorManager.unregisterListener(this);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return;

		if (!isRunning) {accelSensors[0] = event.values[0];accelSensors[1] = event.values[1];accelSensors[2] = event.values[2];isRunning = true;}


		float dx = (event.values[0] - accelSensors[0])/(event.timestamp - prevMovementTime);if (Math.abs(dx) > Math.abs(mx)) mx = dx;
		float dy = (event.values[1] - accelSensors[1])/(event.timestamp - prevMovementTime);if (Math.abs(dy) > Math.abs(my)) my = dy;
		float dz = (event.values[2] - accelSensors[2])/(event.timestamp - prevMovementTime);if (Math.abs(dz) > Math.abs(mz)) mz = dz;


		if (Math.abs(dx) > MAX_CHANGE_X || Math.abs(dy) > MAX_CHANGE_Y || Math.abs(dz) > MAX_CHANGE_Z)
		{
			prevMovementTime = event.timestamp;
			if (prevMovementTime - startMovementTime > 5000000000f && !alarmSounding)
				soundAlarm(event.timestamp);
		}
		else startMovementTime = prevMovementTime = event.timestamp;
	}

	private void soundAlarm(float timestamp)
	{
		startMovementTime = prevMovementTime = timestamp;
		alarmSounding = true;

		startActivity(new Intent(getApplicationContext(),EnterCode.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

		if (alarmSounding)
		{
			alarm = new Thread(new Runnable() {

				@Override
				public void run() {
					try
					{
						mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
						try
						{
							int delay = AntiTheftMain.delay()*1000;
							Thread.sleep(delay);
							AudioManager am = ((AudioManager) getSystemService(Context.AUDIO_SERVICE));
							while (alarmSounding)
							{
								if(!mWakeLock.isHeld()) {
									mWakeLock.acquire();
								}
								if (!mp.isPlaying())
									mp.start();
								am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
								am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
								Thread.sleep(500);
							}
							mWakeLock.release();
							mp.stop();
						}
						catch (Exception e)
						{int x = 6;}
					}
					catch (Exception e)
					{}
				}
			});
			alarm.start();
		}
	}

	protected static void silence()
	{
		alarmSounding = false;
	}
}
